package com.gree.flowable.app.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gree.flowable.app.mapper.CreateTablesMapper;
import com.gree.flowable.app.mapper.FormInfoMapper;
import com.gree.flowable.app.mapper.FormModelMapper;
import com.gree.flowable.app.pojo.form.FormInfo;
import com.gree.flowable.app.pojo.form.FormModel;
import com.gree.flowable.app.services.IFormExportExcelService;
import com.gree.flowable.app.tools.utils.DuridConfig;
import com.gree.flowable.app.tools.utils.ExcelExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/5/10 15:38
 * @description：批量创建单的导出模板
 * @modified By：
 * @version: 1.0
 */
@Service
public class IFormExportExcelServiceImpl implements IFormExportExcelService {

    @Autowired
    private CreateTablesMapper createTablesMapper;

    @Autowired
    private DuridConfig duridConfig;

    @Autowired
    private FormInfoMapper formInfoMapper;

    @Autowired
    private FormModelMapper formModelMapper;
    /**
     * 将表单数据转为Excel并导出
     * @param formId        表单id
     * @param processKey    流程定义id
     * @return
     */
    @Override
    public ResponseEntity<byte[]> exportFormExcel(String formId, String processKey) throws Exception {
        String url = duridConfig.getUrl();
        boolean isMysql = url.contains("mysql");
        /*List<MySQLColumnVO> mySQLColumnVOList = new LinkedList<>();
        if (isMysql){
            mySQLColumnVOList = createTablesMapper.queryColumnsInMySQL(formId);
        }*/
        QueryWrapper<FormInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.lambda().eq(FormInfo::getFormId,formId);
        FormInfo formInfo = formInfoMapper.selectOne(infoQueryWrapper);
        String excelName = formInfo.getFormName();

        List<Map<String,String>> resultColumnList = new LinkedList<>();
        //设置单据主题
        Map<String,String> themeMap = new HashMap<>(16);
        themeMap.put("field","formName");
        themeMap.put("name","单据主题formName");
        resultColumnList.add(0,themeMap);
        //获取导出Excel的list集合
        QueryWrapper<FormModel> formModelQueryWrapper = new QueryWrapper<>();
        formModelQueryWrapper.lambda().eq(FormModel::getFormId,formId).orderByAsc(FormModel::getSort);
        List<FormModel> formModelList = formModelMapper.selectList(formModelQueryWrapper);
        for (FormModel formModel :formModelList) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("field", formModel.getFields());
            resultMap.put("name", formModel.getLabel()+formModel.getFields());
            resultColumnList.add(formModel.getSort(),resultMap);
            /*for (MySQLColumnVO columnVo : mySQLColumnVOList) {
                Map<String,String> resultMap = new HashMap<>();
                //字段key
                String columnField = columnVo.getColumnName();
                //字段的中文描述
                String columnName = columnVo.getColumnComment();
                if (!"business_key".equals(columnField)){
                    resultMap.put("field",columnField);
                    resultMap.put("name",columnName+columnField);
                }
                if (formModel.getFields().equals(columnField)){
                    Integer sort = formModel.getSort();
                    resultColumnList.add(sort,resultMap);
                }

            }

        }*/

        }
        return ExcelExportUtil.exportExcelUtil(resultColumnList,excelName,formId);
    }
}
